import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
mpl.rcParams['font.sans-serif'] = ['SimHei']
mpl.rcParams['font.serif'] = ['SimHei']
mpl.rcParams['axes.unicode_minus'] = False
font_label = {'weight': 'normal',
             'size': 18,
             }
font_legend = {
             'weight': 'normal',
             'size': 18,
             }
def load_metrix(name):
    return pd.read_csv('run/' + name + '.csv', encoding='utf-8')


def plot_energe_with_traffic():
    fig = plt.figure(figsize=(12, 8))
    metrix = load_metrix('log')
    traffic = metrix['traffic']
    energes = metrix['Energes']
    d = '2021-3-4 17:00'
    x = pd.date_range(d, periods=len(traffic), freq='30min')
    ax = fig.add_subplot(111)
    plot1 = ax.plot(x, traffic*10+100, label='流量曲线', ls='--')
    ax.set_ylabel('流量负载',font_label)

    ax2 = ax.twinx()
    plot2 = ax2.plot(x, energes*10, 'r', label='能耗曲线')
    ax2.set_ylabel('系统能耗(W)',font_label)
    ax2.set_xlabel('时间',font_label)

    lines = plot1 + plot2
    ax.legend(lines, [l.get_label() for l in lines],prop=font_legend)

    date_format = mpl.dates.DateFormatter("%H:%M")
    ax.xaxis.set_major_formatter(date_format)
    ax.tick_params(labelsize=15)
    labels = ax.get_xticklabels() + ax.get_yticklabels()
    ax2.tick_params(labelsize=15)
    labels2 = ax2.get_xticklabels() + ax2.get_yticklabels()
    [label.set_fontname('Times New Roman') for label in labels]
    [label.set_fontname('Times New Roman') for label in labels2]
    fig.autofmt_xdate()
    fig = plt.gcf()
    fig.savefig('images/plot_energe_with_traffic',  bbox_inches='tight')
    plt.show()


def plot_loadRate_with_PacketLossRate():
    metrix = load_metrix('log')
    plrs = metrix['Packet loss rate']
    lrs = metrix['load rate']
    fns = metrix['FN_nums']
    d = '2021-3-4 17:00'
    x = pd.date_range(d, periods=len(plrs), freq='30min')

    fig = plt.figure(figsize=(12,8))
    ax = fig.add_subplot(111)
    plot1 = ax.plot(x, lrs, label='FN的平均流量负载')
    plot2 = ax.plot(x, plrs, label='中断概率')
    ax.set_ylabel('负载率',font_label)

    ax2 = ax.twinx()
    plot3 = ax2.bar(x, fns+3, 0.01, label='基站数量',alpha=0.5)
    ax2.set_ylabel('“ON”状态基站数量',font_label)
    ax2.set_xlabel('时间',font_label)
    ax2.set_ylim([2,10])
    lines = plot1 + plot2
    ax.legend(lines, [l.get_label() for l in lines],loc='upper right',prop=font_legend)
    ax2.legend(loc='upper left',prop=font_legend)
    # plt.plot(x, lrs, label='average load rate for FNs')
    # plt.plot(x, plrs, label='total packet loss rate for FNs')
    # plt.legend()
    date_format = mpl.dates.DateFormatter("%H:%M")
    ax.tick_params(labelsize=15)
    labels = ax.get_xticklabels() + ax.get_yticklabels()
    ax2.tick_params(labelsize=15)
    labels2 = ax2.get_xticklabels() + ax2.get_yticklabels()
    [label.set_fontname('Times New Roman') for label in labels]
    [label.set_fontname('Times New Roman') for label in labels2]

    ax.xaxis.set_major_formatter(date_format)
    fig = plt.gcf()
    fig.savefig('images/plot_loadRate_with_PacketLossRate', bbox_inches='tight')
    plt.show()


if __name__ == '__main__':
    plot_loadRate_with_PacketLossRate()
    # plot_energe_with_traffic()
